#!/usr/bin/env python3
# generate commands based on the snapshot xlsx file
import openpyxl

EXCEL_FILE = '/Volumes/Coding/ohscript/committer/committers-snapshot.xlsx'

# 读取Excel文件
wb = openpyxl.load_workbook(EXCEL_FILE, data_only=True)
sheet = wb['Sheet']

def compare_lists_ordered(old_list, new_list):
    """
    比较两个列表，找出新增和删除的元素（保持顺序）
    """
    # 找出新增的元素
    added = [item for item in new_list if item not in old_list]
    
    # 找出删除的元素
    removed = [item for item in old_list if item not in new_list]
    
    return added, removed


def generate_commands():
    commands = []
    # 遍历所有行
    for row in sheet.iter_rows(min_row=2, values_only=True):
        # 检查第2列和第3列是否有变更（索引从0开始）
        if len(row) > 3 and row[2] != row[3]:
            file = row[0]
            old_committers = row[2].strip().split(',') if row[2] else []
            new_committers = row[3].strip().split(',') if row[3] else []
            added, removed = compare_lists_ordered(old_committers, new_committers)
            for ele in removed:
                commands.append(f"python3 committer_tool.py -i ../conf/committers.json -p {file} -d {ele}")
            for ele in added:
                commands.append(f"python3 committer_tool.py -i ../conf/committers.json -p {file} -a {ele}")
    with open('committer-update-commands.sh', 'w') as f:
        for cmd in commands:
            f.write(cmd + '\n')
    print("命令已生成到 committer-update-commands.sh 文件中。")


if __name__ == "__main__":
    generate_commands()

